/*-----------------------------------------------------------------------------
    
    Copyright (C) 2005 T2-Design Corp. All rights reserved.
    
    File: obj_multitext.c
    
    Content: 
    
    History: 
-----------------------------------------------------------------------------*/
#include <sys_config.h>

#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libosd/osd_common_draw.h>
#include "osd_lib_internal.h"

//---------------------------- PRIVATE VARIABLES ----------------------------//
#define BUG_LIBC 	PRINTF	//libc_printf	//Ben 121129#1
#define RIGHT_LIBC 	PRINTF	//libc_printf		//Ben 121205#1
//UINT16 OSD_RebuildStr(PMULTI_TEXT pCtrl,UINT8 *pStr,UINT8 *pRebuildStr);
static UINT16 OSD_RebuildStrExt(PMULTI_TEXT pCtrl,UINT8 *pStr,UINT8 *pRebuildStr);
//UINT16 str_offset;	//Just For debug
//Ben 121205#1
static UINT16 ori_newline;
static UINT16 lineWidth[16];
static struct StrNewLine
{
	UINT16 WordCnts;
	UINT16 LineCnts;			//Total Lines After Rebuild
	UINT16 RealLastLine;	
	UINT16 WordLenBuf[200];
	UINT16 WordWBuf[200];
} Arabic_newStr;
//Arabic_newStr
//Ben 121205#1
//#define BLD_DEBUG

static char word_end_char[] = 
{
    ' ', ',', '.', ':', ';','?','!','/','-','(',')','{','}','[',']','|','=','+','&','@','$','_',      
};
static UINT8 RebuildStr[4096];

BOOL IS_WORD_END(UINT16  wc)
{
	UINT32 i,n;

	n = sizeof(word_end_char);
	for(i=0;i<n;i++)
	{
		if(wc == (UINT16)word_end_char[i])
		return TRUE;
	}

	return FALSE;
}
#define C_LINE_GAP  4
//---------------------------- PRIVATE FUNCTIONS ----------------------------//
/*********************************************/

UINT16 OSD_GetWordLen(UINT8* pText, UINT8 font,UINT16* wordwidth,UINT16* wordheight ,UINT16 width)
{
	UINT16 wc,wordlen = 0;
	UINT16 w,h,mh = 0,ww = 0;
	UINT32 cnt;
	UINT16 font_w = 0, font_h = 0;
	
	while( (wc = ComMB16ToWord(pText)) != 0)
	{
		if(IS_NEWLINE(wc))
		{
			*wordwidth = ww;
			*wordheight = mh;
			return wordlen;
		}
		else if((wc > 0x4e00 && wc < 0x9f45)||
			(wc > 0x3000 && wc < 0x303f)||  
			(wc > 0xff00 && wc < 0xffef)||              
			(wc > 0x2e80 && wc < 0x2eff))//chinese
		{
			if(font_w == 0)
			{
				font_w = 36;
				font_h = 36;
				OSD_GetCharWidthHeight(wc, font, &font_w, &font_h);
			}
			ww += font_w;
			mh = font_h;
			wordlen += 1;
			pText += 2;
			break;
		}
		else if(IS_WORD_END(wc) && wordlen>0)
			break;
		w = 10;
		h = 0;
		cnt = 2;
		if(is_thai_unicode(wc))
		{
			struct thai_cell cell;

			cnt = thai_get_cell(pText, &cell);
			if(cnt == 0)
				break;

			OSD_GetThaiCellWidthHeight(&cell, font,&w,&h);
		}
		else
		{
			OSD_GetCharWidthHeight(wc ,font, &w, &h);
		}
		if(mh < h)
			mh = h;
		if((ww+w)<=width)
			ww += w;
		else
			break;
		wordlen += cnt/2;
		pText += cnt;

		if(IS_WORD_END(wc))
			break;
	}
	*wordwidth = ww;
	*wordheight = mh;

	return wordlen;
}

//#ifdef BIDIRECTIONAL_OSD_STYLE
//UINT16 lastLineWidth;
//#endif
#if 0
//cmx1127
void OSD_GetLastLineWidth(UINT8 *pText,UINT8 font)
{
	UINT16 l;
	UINT16 wc,wordlen;
	UINT16 chl;
	//UINT16 wordw,wordh;
	UINT16 font_w,font_h;
	UINT16 line = 0;

	if(pText==NULL)
	return 0;
    
	l = 0;
	chl = 0;
	wc = ComMB16ToWord(pText);
	while(wc!=0 && line<MAX_LINE_NUM)
	{       

		if(IS_NEWLINE(wc))
		{            
			line++;
			l = 0;
			pText += 2;
		}
		else
		{
			 OSD_GetCharWidthHeight(wc, font, &font_w, &font_h);//OSD_GetWordLen(pText,font,&wordw,&wordh,width);
			 wordlen = font_w;
			if(wordlen > 0)
				l+=wordlen;
			pText += 2;
		}
        
		wc = ComMB16ToWord(pText);
		if(l !=0 && wc==0)
		{
			line++;
		}
	}
#ifdef BIDIRECTIONAL_OSD_STYLE
	lastLineWidth = l;
#endif
}
#endif


UINT16 OSD_GetTextTotalLine(UINT8* pText,UINT8 font,UINT16 width,
                              INT16 lineidx,UINT8**  lineStr, UINT8* linesheght)
{
	UINT16 l;
	UINT16 wc,wordlen;
	//UINT16 maxw,maxh;
	UINT16 chl;
	UINT16 wordw,wordh;
	UINT16 line = 0;

	if(pText==NULL)
	return 0;
    
	l = 0;
	chl = 0;
	wc = ComMB16ToWord(pText);
	while(wc!=0 && line<MAX_LINE_NUM)
	{       
		if(l==0 && line == lineidx  && lineStr!=NULL)
			*lineStr = pText;

		if(IS_NEWLINE(wc))
		{            
			if(chl== 0)
				chl = OSD_FONT_HEIGHT;
			if(linesheght != NULL)
				linesheght[line] = chl;
			lineWidth[line] = l;	//Ben 121205#1
			line++;
			l = 0;
			chl = 0;
			pText += 2;
		}
		else
		{
			wordlen = OSD_GetWordLen(pText,font,&wordw,&wordh,width);
			if(wordlen > 0)
			{
				if(chl== 0)
					chl = wordh;

				/*If first char of the line is a space, ignore it.*/
				//                if(l == 0 && wordlen==1 && wc == ' ')
				//               {
				//                    pText += 2;
				//                }
				//                else if(l + wordw <= width || l == 0)
				if(l + wordw <= width || l == 0)
				{
					l += wordw;
					pText += wordlen*2;
				}
				else    /* Next line*/
				{
					if(chl < wordh)
						chl = wordh;
					if(chl== 0)
						chl = OSD_FONT_HEIGHT;
					if(linesheght != NULL)
						linesheght[line] = chl;
					lineWidth[line] = l;	//Ben 121205#1
					line++;
					l = 0;
					chl = 0;
				}
			}
		}
        
		wc = ComMB16ToWord(pText);
		if(l !=0 && wc==0)
		{
			if(chl== 0)
				chl = OSD_FONT_HEIGHT;
			if(linesheght != NULL)
				linesheght[line] = chl;
			lineWidth[line] = l;	//Ben 121205#1
			line++;
		}
	}
//#ifdef BIDIRECTIONAL_OSD_STYLE
//	lastLineWidth = l;
//#endif
	return line;
}

//Ben 121129#1	
void OSD_GetTopLine(PMULTI_TEXT pCtrl,UINT8 *pStr,INT16 lineidx,UINT8**  lineStr,UINT8* linesheght)
{
	UINT16 l;
	UINT16 wc,wordlen;
	UINT16 chl;
	UINT16 wordw,wordh;
	UINT16 line = 0;
    
	l = 0;
	wc = ComMB16ToWord(pStr);
	while(wc!=0 && line<MAX_LINE_NUM)
	{
		if(l==0 && line == lineidx  && lineStr!=NULL)
			*lineStr = pStr;

		if(IS_NEWLINE(wc))
		{       
			if(chl== 0)
				chl = OSD_FONT_HEIGHT;
			if(linesheght != NULL)
				linesheght[line] = chl;		
			line++;
			RIGHT_LIBC("line=%d \n",line);	//Ben 121130#1
			l = 0;
			chl = 0;
			pStr += 2;
		}
		else
		{
			wordlen = OSD_GetWordLen(pStr,pCtrl->head.bFont,&wordw,&wordh,pCtrl->rcText.uWidth);
			if(wordlen > 0)
			{
				if(chl== 0)
					chl = wordh;			
				if(l + wordw <= pCtrl->rcText.uWidth || l == 0)
				{
					l += wordw;
					pStr += wordlen*2;
				}
				else    /* Next line*/
				{
					if(chl < wordh)
						chl = wordh;
					if(chl== 0)
						chl = OSD_FONT_HEIGHT;
					if(linesheght != NULL)
						linesheght[line] = chl;				
					line++;
					l = 0;
				}
			}
		}
			
		wc = ComMB16ToWord(pStr);

		if(l !=0 && wc==0)
		{
			if(chl== 0)
				chl = OSD_FONT_HEIGHT;
			if(linesheght != NULL)
				linesheght[line] = chl;		
			line++;
		}
	}
	//Ben 121130#1
//#ifdef BIDIRECTIONAL_OSD_STYLE
//	lastLineWidth = l;
//#endif	
	//Ben 121130#1
}
//Ben 121129#1

void OSD_GetMTextInfor(PMULTI_TEXT pCtrl,mtxtinfo_t * pInfo)
{
	PTEXT_CONTENT pText;
	UINT16 width,height;
	UINT16 total_lines = 0,page_lines = 0,lines;
	INT16  lidx;
	UINT32 i;
	UINT8 *pStr;
	
	width  = pCtrl->rcText.uWidth;
	page_lines = 0;

	for(i=0; i<pCtrl->bCount; i++)
	{
		pText = &pCtrl->pTextTable[i];

		if(pText == NULL)
			continue;

		if(pText->bTextType == (UINT8)STRING_ID)
			pStr = OSD_GetUnicodeString(pText->text.wStringID);
		else
			pStr = (UINT8*)pText->text.pString;
		if(pStr== NULL)
			continue;

		RIGHT_LIBC("topline_idx=%d total_lines=%d \n",pInfo->topline_idx,total_lines);		//Ben 121204#1
		lidx = (INT16)pInfo->topline_idx - (INT16)total_lines;

		RIGHT_LIBC("OSD_GetMTextInfor:lidx=%d \n",lidx);
		if(OSD_CheckRebuildStr(pStr) != FALSE)
		{
			MEMSET(RebuildStr,0,4096);
			//lines = OSD_RebuildStr(pCtrl,pStr,RebuildStr);
			lines = OSD_RebuildStrExt(pCtrl,pStr,RebuildStr);
			OSD_GetTopLine(pCtrl,RebuildStr,lidx,&pInfo->topline_pstr,&pInfo->line_height[total_lines]);
			
			RIGHT_LIBC("topline_pstr=0x%08x \n",pInfo->topline_pstr);
			//OSD_GetLastLineWidth(pInfo->topline_pstr,pCtrl->head.bFont);	//Ben 121130#1
		}
		else
			lines = OSD_GetTextTotalLine(pStr,pCtrl->head.bFont,width,lidx,&pInfo->topline_pstr,&pInfo->line_height[total_lines]);
		//Ben 121129#1	
		
		if(pInfo->topline_idx >= total_lines 
			&& pInfo->topline_idx< lines + total_lines)
			pInfo->topline_tblidx = i;        
		total_lines += lines;
	}

	/* Total lines */
	pInfo->total_lines = total_lines;

	/* Get the total logical height.*/
	pInfo->total_height = 0;
	for(i=0;i<total_lines;i++)
	{
		pInfo->total_height += pInfo->line_height[i];
		if( i> 0)
			pInfo->total_height += C_LINE_GAP;
	}

	/* One page's height */
	pInfo->page_height = pCtrl->rcText.uHeight;

	if(pInfo->topline_idx >= total_lines)
		pInfo->topline_idx = 0;

#if 0
    /* Check the topline_idx */
CHECK_TOP_LINE: 
    height = 0;
    for(i=pInfo->topline_idx;i<total_lines;i++)
    {
        height += pInfo->line_height[i];
        if(i != pInfo->topline_idx)
            height += C_LINE_GAP;
    }
    if(pInfo->topline_idx> 0 && height < pInfo->page_height)
    {
        if(height + pInfo->line_height[total_lines - 1] + C_LINE_GAP <= pInfo->page_height)
        {
            pInfo->topline_idx -= 1;        
            goto CHECK_TOP_LINE;
        }
    }
#endif

    /* Get Current Page's lines */
	height = 0;
	//for(i=pInfo->topline_idx;i<total_lines;i++)
	for(i=0;i<total_lines;i++)
	{
		height += pInfo->line_height[i];
		if(i != pInfo->topline_idx)
			height += C_LINE_GAP;
		if(height > pInfo->page_height)
			break;
	}

	page_lines = i;
	pInfo->page_lines  = page_lines;    
}

//Ben 121205#1
//return total lines
//Just Handle A long Str without '\n',Return Length After Rebuild.
static UINT16 OSD_RebuildLongStr(PMULTI_TEXT pCtrl,UINT8 *pStr,UINT8 *pRebuildStr,struct StrNewLine *NewLine)
{
	UINT16 l;
	UINT16 j = 0,k = 0,m = 0,n = 0;
	UINT16 cntLine = 0,StrLen=0;
	UINT16 StrLenLast;	//,ori_Lastlinewidth;
	UINT16 StrLineFlag[MAX_LINE_NUM];
	UINT16 Lastlinewidth_tmp;
	UINT16 LengthRe;
	
	MEMSET(StrLineFlag,0,MAX_LINE_NUM);
	
    	j = NewLine->WordCnts;
	k = 0;
    	l = 0;
	BUG_LIBC(">>> j=%d \n",j);
	
    	while(j--)
    	{
        	if( l + NewLine->WordWBuf[j] > pCtrl->rcText.uWidth )
        	{
        		lineWidth[ori_newline+cntLine] = l;
			RIGHT_LIBC("Width=%d lineth=%d \n",l,ori_newline+cntLine);
			
            		StrLineFlag[cntLine++] = k*2;
			BUG_LIBC(">>> StrLineFlag[%d]=%d j=%d \n",cntLine,k*2,j);
            		l = NewLine->WordWBuf[j];
            		k = NewLine->WordLenBuf[j];
			BUG_LIBC(">>> l=%d k=%d \n",l,k);		
        	}
        	else
        	{
            		l += NewLine->WordWBuf[j];
            		k += NewLine->WordLenBuf[j];
			//BUG_LIBC(">>> l=%d k=%d \n",l,k);	
        	}	
    	}
	//ori_Lastlinewidth = l;
	lineWidth[ori_newline+cntLine] = l;
	RIGHT_LIBC("Width=%d last lineth=%d \n",l,ori_newline+cntLine);
	
	//BUG_LIBC(">>> ori_Lastlinewidth=%d \n",ori_Lastlinewidth);	
	
	#if 0
	UINT16 wordw,wordh,wordlen,space_add;
	space_add = 0;
	if(0)		//For Debug Right Align
	//if(0 == NewLine->RealLastLine)
	{
    		wordlen = OSD_GetWordLen(" ",pCtrl->head.bFont,&wordw,&wordh,pCtrl->rcText.uWidth);
		space_add = (pCtrl->rcText.uWidth - ori_Lastlinewidth)/wordw;		//Need Add bI " "
		BUG_LIBC("space width=%d space_add=%d cntLine =%d\n",wordw,space_add,cntLine);
	}
	#endif

	LengthRe = 0;
	//Need Change Line?
	if(0 == cntLine)
	{
		StrLenLast = k*2;
		//For Debug Double CHeck last line width,If OK Pls Mark.
		//#define DEBUG_LASTL
		#ifdef DEBUG_LASTL
    		Lastlinewidth_tmp = 0;
		for(j=0; j<StrLenLast; j++)
			Lastlinewidth_tmp += NewLine->WordWBuf[j+k];
		BUG_LIBC("0__Lastlinewidth_tmp=%d ori_Lastlinewidth=%d\n",Lastlinewidth_tmp,ori_Lastlinewidth);	
		#endif
		//
		#if 0
		for(j=0; j<space_add*2;)
		{
			pRebuildStr[j] = '\0';
			pRebuildStr[j+1] = ' ';
			j+=2;
		}
		LengthRe += space_add*2;
		for(j=0; j<StrLenLast; j++)
			pRebuildStr[j+2*space_add] = pStr[j];			
		#else
		for(j=0; j<StrLenLast; j++)
			pRebuildStr[j] = pStr[j];		
		#endif
		
		LengthRe += StrLenLast;

		//Add "\n" Or '\0'
		pRebuildStr[LengthRe] = '\0';
		if(NewLine->RealLastLine)
			pRebuildStr[LengthRe+1] ='\0';
		else
			pRebuildStr[LengthRe+1] ='\n';
		
		LengthRe += 2;
		NewLine->LineCnts = 1;
		BUG_LIBC("0__LengthRe=%d LineCnts=%d \n",LengthRe,NewLine->LineCnts);	
		return LengthRe;
	}
	
	//Get Total Length
	j = NewLine->WordCnts;
	while( j-- )
	{
	        StrLen += NewLine->WordLenBuf[j]*2;
	}
	LengthRe += StrLen;	
	
	//The Last Line's word num.	
	NewLine->LineCnts = cntLine+1;
	j = cntLine;
	k = 0;	
	BUG_LIBC(">>> cntLine=%d \n",cntLine);
	while( j-- )
	{
	     	k += StrLineFlag[j];
	}
	BUG_LIBC(">>> StrLen=%d k=%d \n",StrLen,k);	
	StrLenLast = StrLen - k;
	//For Debug Double CHeck last line width,If OK Pls Mark.
	//#define DEBUG_LASTL
	#ifdef DEBUG_LASTL
    	Lastlinewidth_tmp = 0;
	BUG_LIBC("StrLenLast=%d k=%d\n",StrLenLast,k);	
	for(j=0; j<StrLenLast; j++)
		Lastlinewidth_tmp += NewLine->WordWBuf[j+k];
	BUG_LIBC("M__Lastlinewidth_tmp=%d ori_Lastlinewidth=%d\n",Lastlinewidth_tmp,ori_Lastlinewidth);	
	#endif
	//	

	//String Rebuild except the last line.
	j = 0;
    	while( ++j )
    	{
        	if( j > cntLine ) break;
        	k = StrLineFlag[j-1];
		BUG_LIBC("M__start copy to offset=%d \n",StrLineFlag[j-1]-k+m+n*2);	
        	while(k)
        	{
            		pRebuildStr[StrLineFlag[j-1]-k+m+n*2] = pStr[StrLen-k-m];
			k--;				
        	}
        	m += StrLineFlag[j-1];
		BUG_LIBC("M__Add Line end=%d offset=%d\n",j,m+2*n);	
        	pRebuildStr[m+n*2] = '\0';
        	pRebuildStr[m+n*2+1] = '\n';
		LengthRe += 2;	//Ben 121205#1	
        	n++;
    	}
    
	//If With Last line,Rebuild The Last Line.
    	if( StrLenLast )
    	{
    		//Space Add
    		#if 0
    		if(0 == NewLine->RealLastLine)
    		{
			for(j=0;j<space_add*2;)
			{
				pRebuildStr[j+m+n*2] = '\0';
				pRebuildStr[j+1+m+n*2] = ' ';
				j+=2;
			}
			LengthRe += space_add*2;
    		}
        	j = StrLenLast;
		BUG_LIBC("M__Add Line end offset=%d StrLenLast=%d \n",
					StrLen - j-1+n*2+2*space_add,j);
        	while( j-- )
        	{
            		pRebuildStr[StrLen - j-1+n*2+2*space_add] = pStr[StrLenLast - j-1];	
        	}
        	pRebuildStr[StrLen+n*2+2*space_add] = '\0';
		
		if(NewLine->RealLastLine)	
			pRebuildStr[StrLen+n*2+1+2*space_add] = '\0';
		else
		{
			BUG_LIBC("M__Add last Line end offset=%d\n",StrLen+n*2+2*space_add);
        		pRebuildStr[StrLen+n*2+1+2*space_add] = '\n';
		}
		#else
        	j = StrLenLast;
		BUG_LIBC("M__Add Line end offset=%d StrLenLast=%d \n",
					StrLen - j-1+n*2,j);
        	while( j-- )
        	{
            		pRebuildStr[StrLen - j-1+n*2] = pStr[StrLenLast - j-1];	
        	}
        	pRebuildStr[StrLen+n*2] = '\0';
		
		if(NewLine->RealLastLine)	
			pRebuildStr[StrLen+n*2+1] = '\0';
		else
		{
			BUG_LIBC("M__Add last Line end offset=%d\n",StrLen+n*2);
        		pRebuildStr[StrLen+n*2+1] = '\n';
		}		
		#endif
		//
    	}

	LengthRe += 2;
	BUG_LIBC("M__LengthRe=%d LineCnts=%d \n",LengthRe,NewLine->LineCnts);	
	return LengthRe;	
	//
}

static UINT16 OSD_RebuildStrExt(PMULTI_TEXT pCtrl,UINT8 *pStr,UINT8 *pRebuildStr)
{
	UINT16 wc,wordlen;
	UINT16 wordw,wordh;
	UINT16 cntLine = 0;
	UINT16 j = 0,k = 0,m = 0,n = 0;
	UINT8 *pStrTmp;
	//UINT16 ori_newline = 0;
	UINT16 ori_linelength,bI,str_offset;

	MEMSET(&Arabic_newStr,0,sizeof(Arabic_newStr));
	
	ori_newline = 0;		//Ben 121205#1
	ori_linelength = 0;
	str_offset = 0;
	
	pStrTmp = pStr;
	
	wc = ComMB16ToWord(pStrTmp);
	//BUG_LIBC("print wc=0x%04x \n",wc);
	while(wc!=0)	
	{
		if(IS_NEWLINE(wc))
		{
			BUG_LIBC("ori new line wc=0x%04x\n",wc);
			pStrTmp += 2;
			//Save
			j = Arabic_newStr.WordCnts;
			Arabic_newStr.RealLastLine = 0;
			str_offset += OSD_RebuildLongStr(pCtrl,pStr+ori_linelength,pRebuildStr+str_offset,&Arabic_newStr);
			BUG_LIBC("Rebuild  str_offset=%d \n",str_offset);
			
			for(bI=0;bI<j;bI++)
				ori_linelength += Arabic_newStr.WordLenBuf[bI]*2;
			ori_linelength += 2;	//Include '\n'
			
			ori_newline += Arabic_newStr.LineCnts;

			MEMSET(&Arabic_newStr,0,sizeof(Arabic_newStr));	
			BUG_LIBC("ori_linelength=%d str_offset=%d\n",ori_linelength,str_offset);
		}
		else
		{
			wordlen = OSD_GetWordLen(pStrTmp,pCtrl->head.bFont,&wordw,&wordh,pCtrl->rcText.uWidth);
			//BUG_LIBC("print wordlen=0x%04x\n",wordlen);
			if(wordlen > 0)
			{
                		j = Arabic_newStr.WordCnts;
                		Arabic_newStr.WordLenBuf[j]=wordlen;
                		Arabic_newStr.WordWBuf[j]=wordw;
				//BUG_LIBC("wordw=%d WordCnts=%d \n",wordw,Arabic_newStr->WordCnts);	
                		pStrTmp += wordlen*2;
                		Arabic_newStr.WordCnts++;
			}
		}
        
		wc = ComMB16ToWord(pStrTmp);	
	}

	BUG_LIBC("Rebuild Last Line\n");
	Arabic_newStr.RealLastLine = 1;
	str_offset = OSD_RebuildLongStr(pCtrl,pStr+ori_linelength,pRebuildStr+str_offset,&Arabic_newStr);
	ori_newline += Arabic_newStr.LineCnts;
	BUG_LIBC("Rebuild Last str_offset=%d ori_newline=%d \n",str_offset,ori_newline);
	
	#ifdef BLD_DEBUG
	UINT16 bJ = 0;
	libc_printf("After Build \n");	
       while(1)
	{
		wc = ComMB16ToWord(pRebuildStr+bJ);
		
		libc_printf("wc=0x%04x ",wc);
		bJ+=2;
		if((bJ%16) == 0)
			libc_printf("\n ");
		if(wc == 0x000a)
			libc_printf("\n change flag bJ=%d \n ",bJ);
		if(wc == 0)
		{   
			libc_printf("\n end flag bJ=%d \n ",bJ);
                	break;
		}
       }
	#endif

	return ori_newline;
}

//Ben 121205#1
#if 0
UINT16 OSD_RebuildStr(PMULTI_TEXT pCtrl,UINT8 *pStr,UINT8 *pRebuildStr)
{
    	UINT16 l;
	UINT16 wc,wordlen;
	UINT16 wordw,wordh;
	UINT16 line = 0;
    	UINT16 StrLenBuf[200],StrWordWBuf[200];
    	UINT16 StrLineFlag[MAX_LINE_NUM];
    	UINT16 cntBuf = 0,cntLine = 0;
    	UINT16 StrLen = 0;
    	UINT16 StrLenLast = 0;
    	UINT8 *pStrTmp;
    	UINT16 j = 0,k = 0,m = 0,n = 0;
	UINT16 lines = 0;	
	//Ben 121130#1
	UINT16 ori_newline = 0;	
	UINT8 ori_baseline[200];
	UINT8 *ori_str;
	UINT16 ori_linelength,bI,ori_linewidth,space_add,str_offset;
	ori_linelength = 0;
	ori_linewidth = 0;
	str_offset = 0;
	//Ben 121130#1
	    
	l = 0;
    	pStrTmp = pStr;
	ori_str = 	pStr;
	wc = ComMB16ToWord(pStrTmp);
	BUG_LIBC("print wc=0x%04x ori_str=0x%08x \n",wc,ori_str);		//Ben 121127#1
	while(wc!=0)
	{       
		if(IS_NEWLINE(wc))
		{    
			BUG_LIBC("ori new line wc=0x%04x\n",wc);		//Ben 121127#1
			pStrTmp += 2;
			//Ben 121130#1
			j = cntBuf;
			ori_linewidth = 0;
			space_add = 0;
			for(bI=0;bI<j;bI++)
				ori_linewidth += StrWordWBuf[bI];

			for(bI=0;bI<j;bI++)
				ori_linelength += StrLenBuf[bI]*2;

			str_offset = ori_linelength+2;	//Incloud "\n"
			BUG_LIBC("ori_linewidth=%d ori_linelength=%d\n",ori_linewidth,ori_linelength);		//Ben 121127#1
			wordlen = OSD_GetWordLen(" ",pCtrl->head.bFont,&wordw,&wordh,pCtrl->rcText.uWidth);

			//Ben 121203#1
			while(ori_linewidth > pCtrl->rcText.uWidth)
			{
				ori_linewidth -= pCtrl->rcText.uWidth;
			}
			//Ben 121203#1
			space_add = (pCtrl->rcText.uWidth - ori_linewidth)/wordw;	//Need Add bI " "
			BUG_LIBC("space width=%d space_add=%d\n",wordw,space_add);		//Ben 121127#1
			
			ori_newline++;
			cntBuf = 0;
			//j = cntBuf;
			//ori_linelength += 2*space_add;
			//ori_linelength = pStrTmp-ori_str;
			//memcpy(ori_baseline,ori_str, ori_linelength);
			//Ben 121130#1
		}
		else
		{
			wordlen = OSD_GetWordLen(pStrTmp,pCtrl->head.bFont,&wordw,&wordh,pCtrl->rcText.uWidth);
			BUG_LIBC("print wordlen=0x%04x\n",wordlen);		//Ben 121127#1
			if(wordlen > 0)
			{
                		j = cntBuf;
                		StrLenBuf[j]=wordlen;
                		StrWordWBuf[j]=wordw;
				BUG_LIBC("wordw=%d cntBuf=%d \n",wordw,cntBuf);		//Ben 121127#1	
                		pStrTmp += wordlen*2;
                		cntBuf++;
			}
		}
        
		wc = ComMB16ToWord(pStrTmp);
		BUG_LIBC("print wc=0x%04x\n",wc);		//Ben 121127#1
	}
    	j = cntBuf;
    	k = 0;
	BUG_LIBC(">>> j=%d \n",j);		//Ben 121127#1
	BUG_LIBC("uWidth %d \n",pCtrl->rcText.uWidth);		//Ben 121127#1
    	while(j--)
    	{
        	if( l + StrWordWBuf[j] > pCtrl->rcText.uWidth )
        	{
            		StrLineFlag[cntLine++] = k*2;
			BUG_LIBC(">>> StrLineFlag=%d cntLine=%d \n",StrLineFlag[cntLine],cntLine);		//Ben 121127#1		
            		l = StrWordWBuf[j];
            		k = StrLenBuf[j];
			BUG_LIBC(">>> l=%d k=%d \n",l,k);		//Ben 121127#1		
        	}
        	else
        	{
            		l += StrWordWBuf[j];
            		k += StrLenBuf[j];
			BUG_LIBC(">>> l=%d k=%d \n",l,k);		//Ben 121127#1		
        	}
    	}
	
    	j = cntBuf;
    	while( j-- )
    	{
        	StrLen += StrLenBuf[j]*2;		//Total Length
    	}
    	j = cntLine;
	lines = cntLine+ori_newline;		//Ben 121130#1
	BUG_LIBC(">>> cntLine=%d \n",cntLine);		//Ben 121127#1
	
    	k = 0;
    	while( j-- )
    	{
        	k += StrLineFlag[j];
    	}
    	StrLenLast = StrLen - k;
    	j = 0;//cntLine;
    	if(cntLine == 0)
        	return 0;

	//Ben 121130#1
	if(ori_newline)
	{
		for(bI=0;bI<space_add*2;)
		{
			pRebuildStr[bI] = '\0';
			pRebuildStr[bI+1] = ' ';
			bI+=2;
		}
		
		for(bI=0; bI<ori_linelength; bI++)
			pRebuildStr[bI+2*space_add] = pStr[bI];

		ori_linelength += 2*space_add;
		//Add "\n"
		pRebuildStr[ori_linelength] = '\0';
		pRebuildStr[ori_linelength+1] ='\n';
		ori_linelength+=2;
	}
	//-----------------------------------------
	
    	while( ++j )
    	{
        	if( j > cntLine ) break;
        	k = StrLineFlag[j-1];
        	while(k)
        	{
            		pRebuildStr[StrLineFlag[j-1]-k+m+n*2+ori_linelength] = pStr[StrLen-k-m+str_offset];
			k--;				
        	}
        	m += StrLineFlag[j-1];
        	//pRebuildStr[m+n*2] = '\0';
        	//pRebuildStr[m+n*2+1] = '\n';
        	pRebuildStr[m+n*2+ori_linelength] = '\0';
        	pRebuildStr[m+n*2+1+ori_linelength] = '\n';			
        	n++;
    	}
    
    
    	if( StrLenLast )
    	{
        	j = StrLenLast;
		bI = 0;	
        	while( j-- )
        	{
            		pRebuildStr[StrLen - j-1+n*2+ori_linelength] = pStr[StrLenLast - j-1+str_offset];	
        	}
        	//pRebuildStr[StrLen+n*2] = '\0';
        	//pRebuildStr[StrLen+n*2+1] = '\0';
        	pRebuildStr[StrLen+n*2+ori_linelength] = '\0';
        	pRebuildStr[StrLen+n*2+1+ori_linelength] = '\0';			
    	}
	lines++;		//For Last Line
	//Ben 121129#1
	//#define BLD_DEBUG
	#ifdef BLD_DEBUG
	UINT16 bJ = 0;
	libc_printf("After Build \n");	
       while(1)
	{
		wc = ComMB16ToWord(pRebuildStr+bJ);
		
		libc_printf("wc=0x%04x ",wc);
		bJ+=2;
		if((bJ%16) == 0)
			libc_printf("\n ");

		if(wc == 0)
		{   
			libc_printf("bJ=%d \n ",bJ);
                	break;
		}
       }
	   #endif
	//Ben 121129#1

	BUG_LIBC("rebuild lines=%d \n",lines);		   
    	return lines;	//Ben 121129#1
}
#endif
//Ben 121205#1

BOOL OSD_CheckRebuildStr(UINT8 *pStr)
{
    	UINT16  wc,cnt1=0,cnt2=0,per=0;
	UINT16 bI = 0;	//Ben 121127#1

	MEMSET(lineWidth,0,16);	 	//Ben 121205#1
       while(1)
	{
		wc = ComMB16ToWord(pStr);
		
		//Ben 121127#1
		#ifdef BLD_DEBUG
		libc_printf("wc=0x%04x ",wc);
		bI++;
		if((bI%8) == 0)
			libc_printf("\n ");
		#endif
		//Ben 121127#1
		
		if(wc == 0)
		{   
			#ifdef BLD_DEBUG
			libc_printf("bI=%d cnt1=%d cnt2=%d\n",bI,cnt1,cnt2);
			#endif
                	if(cnt1 || cnt2)
                	{
                    		if(cnt1 >= cnt2)
                        		return TRUE;
                    		else
                        		return FALSE;
                	}
                	break;
		}
		else
		{   
			//Arabic/farsi unicode
                	if(((wc>=0x600) && (wc<=0x6ff))
                	||((wc>=0x750) && (wc<=0x77f))
                	||((wc>=0x8a0) && (wc<=0x8ff))
                	||((wc>=0xfb50) && (wc<=0xfdff))
                	||((wc>=0xfe70) && (wc<=0xfeff)))
                	{
                    		cnt1++;
                	}
                	else
                	{
                		if(wc != 0x0020)	//Ben 121205#1
                    			cnt2++;
                	}
             
                	pStr += 2;
		}
        }
        return FALSE;
}
//---------------------------- PUBLIC FUNCTIONS -----------------------------//

// public
// if not support malloc, it can only set string_id or unicode_string
void OSD_SetMultiTextContent(PMULTI_TEXT pCtrl, PTEXT_CONTENT pTextTable)
{
        pCtrl->pTextTable = pTextTable;
}

// private
void OSD_DrawMultiTextCell(PMULTI_TEXT pCtrl, UINT8 bStyleIdx,UINT32 nCmdDraw)
{
	lpVSCR 		pVscr;
	struct OSDRect *objframe;	
	//ID_RSC		RscId;
	PWINSTYLE  	lpWinSty;
	UINT32		bgColor;
	UINT8 			bAlign,hAligin,vAlign;
	OBJECTINFO	RscLibInfo;
	struct OSDRect r;

	PTEXT_CONTENT pText;
	UINT16 width,height,left,right,bottom;
	INT16  lidx;
	UINT32 i,k;
	UINT8 *pStr,*pDrawStr,*pDrawStrTmp = NULL;
	UINT16  wc;
	UINT16 wordlen,wordw,wordh,maxW,maxH,charw,charh;
	UINT8 font;
	UINT16 ox,oy,x,y,l,t;
	mtxtinfo_t mtxtinfo;
	UINT32 bNewLine;
	BOOL reverse_flag;
#ifdef BIDIRECTIONAL_OSD_STYLE
	UINT16 lineCount = 1;
	BOOL mirror_flag = g_osd_rsc_info.osd_get_mirror_flag();
#endif
	
	objframe	= &pCtrl->head.frame;
	font        = pCtrl->head.bFont;
	lpWinSty 	= g_osd_rsc_info.osd_get_win_style(bStyleIdx);
	bgColor     = lpWinSty->wBgIdx;
	bAlign 		= pCtrl->bAlign;
	hAligin     = GET_HALIGN(bAlign);
	vAlign      = GET_VALIGN(bAlign);
	
	pVscr = OSD_DrawObjectFrame(objframe,bStyleIdx);
	if(pVscr->lpbScr != NULL)
		bgColor = C_NOSHOW;
    
	r = *objframe;
	r.uLeft += pCtrl->rcText.uLeft;
	r.uTop  += pCtrl->rcText.uTop;
	r.uWidth    = pCtrl->rcText.uWidth;
	r.uHeight   = pCtrl->rcText.uHeight;
	width  = r.uWidth;
	height = r.uHeight;

GET_MULTITEXT_INFOR:
	mtxtinfo.topline_idx = (UINT8)pCtrl->nLine;
	mtxtinfo.topline_pstr = NULL;
	
	OSD_GetMTextInfor(pCtrl,&mtxtinfo);
		
	if(mtxtinfo.topline_pstr == NULL && pCtrl->nLine!=0)
	{
		pCtrl->nLine = 0;
		goto GET_MULTITEXT_INFOR;
	}
	if(mtxtinfo.topline_pstr == NULL)
		return;


	ox = 0;
	if(mtxtinfo.total_lines == 1)
	{
		maxW = OSD_MultiFontLibStrMaxHW(mtxtinfo.topline_pstr,font,&maxW,&maxH,0);
		if(maxW<width)
		{
			if(hAligin==C_ALIGN_RIGHT)
				ox = width - maxW;
			else if(hAligin == C_ALIGN_CENTER)
				ox = (width - maxW)>>1;
		}        
	}
    
	
	if(vAlign == C_ALIGN_TOP)
		oy = 0;
	else if(vAlign == C_ALIGN_BOTTOM)
	{
		if(mtxtinfo.total_height < height)
			oy = height - mtxtinfo.total_height;
		else
			oy = 0;
	}
	else
	{
		if(mtxtinfo.total_height < height)
			oy = (height - mtxtinfo.total_height)>>1;
		else
			oy = 0;
	}
	x = r.uLeft + ox;
	y = r.uTop + oy;
	l = x;
	t = y;
	bottom = r.uTop + height;
	right  = r.uLeft + width;
	left = r.uLeft;
	
	lidx  = mtxtinfo.topline_idx ;
	bNewLine = 0;

	for(i=0; i<pCtrl->bCount; i++)
	{
		/* Draw from the top line*/
		if(i<mtxtinfo.topline_tblidx)
			continue;
         
		pText = &pCtrl->pTextTable[i];
		if(pText == NULL)
			continue;
		
		reverse_flag = FALSE;
		if(pText->bTextType == STRING_ID)
			pStr = OSD_GetUnicodeString(pText->text.wStringID);
		else
		{
			pStr = (UINT8*)pText->text.pString;
			if(pText->bTextType == STRING_REVERSE)
				reverse_flag = TRUE;
		}
		if(pStr == NULL)
			continue;
		
		if(i == mtxtinfo.topline_tblidx)    /* The top line's string pointer.*/
			pDrawStr = mtxtinfo.topline_pstr;
		else
			pDrawStr = pStr;
		
#ifdef BIDIRECTIONAL_OSD_STYLE
//		if (reverse_flag != mirror_flag)
		if (reverse_flag || mirror_flag)
#else
		if (reverse_flag)
#endif
		{
			//force to right align
			x = r.uLeft+r.uWidth;
			l = x;
			//Ben 121205#1
			//First Line Right Alight
			RIGHT_LIBC("reverse_flag=%d mirror_flag= %d \n",reverse_flag,mirror_flag);	
			RIGHT_LIBC("lineWidth[%d] = %d \n",lidx,lineWidth[lidx]);	
			l -=  r.uWidth - lineWidth[lidx];
			//Ben 121205#1
		}
		
		while(1)
		{
			wc = ComMB16ToWord(pDrawStr);
			if(wc == 0)
			{   
				/* If the last character is not a new line 
				character, then set next line from the start.*/
				if(!bNewLine)
				{
					l = x;					
					t += mtxtinfo.line_height[lidx] + C_LINE_GAP;

#ifdef BIDIRECTIONAL_OSD_STYLE
					lineCount ++;
					if (reverse_flag || mirror_flag)
					{
						RIGHT_LIBC("2 lineCount=%d lidx=%d nLine=%d total_lines=%d uWidth=%d \n",
								lineCount,lidx,(UINT8)pCtrl->nLine,mtxtinfo.total_lines,r.uWidth);		//Ben 121127#1

						l -=  r.uWidth - lineWidth[lidx];
					//if ((lidx+lineCount) == mtxtinfo.total_lines)		//Ben 121204#1
    					//if (lineCount == mtxtinfo.total_lines)
    					//	l -= r.uWidth - lastLineWidth;
					}
#endif
				}
				break;
			}
			else if(IS_NEWLINE(wc)) /* New line characters */
			{
				l = x;
				t += mtxtinfo.line_height[lidx++] + C_LINE_GAP;			
				pDrawStr += 2;
				wordlen = 0;
				bNewLine = 1;
#ifdef BIDIRECTIONAL_OSD_STYLE
				lineCount ++;
				if (reverse_flag || mirror_flag)
				{
					RIGHT_LIBC("1 lineCount=%d lidx=%d nLine=%d total_lines=%d uWidth=%d \n",
								lineCount,lidx,(UINT8)pCtrl->nLine,mtxtinfo.total_lines,r.uWidth);		//Ben 121127#1
					//Ben 121205#1
					#if 1
					l -=  r.uWidth - lineWidth[lidx];
					#else
					if (lidx == (mtxtinfo.total_lines-1))		//Ben 121204#1
	    				//if (lineCount == mtxtinfo.total_lines)
	    					l -= r.uWidth - lastLineWidth;
					#endif
				}
#endif
			}
			else
			{   
				/* Get a word */
				wordlen = OSD_GetWordLen(pDrawStr,font,&wordw,&wordh,width);
				if(wordlen>0 && l!=x)
				{
#ifdef BIDIRECTIONAL_OSD_STYLE
//					if(((reverse_flag==mirror_flag)&&(l + wordw > right))||((reverse_flag!=mirror_flag)&&(l<left+wordw)))
					if( ((!(reverse_flag || mirror_flag))&&(l + wordw > right)) \
                        ||(((reverse_flag || mirror_flag))&&(l<left+wordw)) \
                         )  //|| ComUniStrLenExt(pDrawStr) == wordlen
#else
					if((!reverse_flag&&(l + wordw > right))||(reverse_flag&&(l<left+wordw)))
#endif
					{
						l = x;
						t += mtxtinfo.line_height[lidx++] + C_LINE_GAP;				

						bNewLine = 1;
#ifdef BIDIRECTIONAL_OSD_STYLE
						lineCount ++;
						if (reverse_flag || mirror_flag)
						{
						RIGHT_LIBC("0 lineCount=%d lidx=%d nLine=%d total_lines=%d uWidth=%d  \n",
								lineCount,lidx,(UINT8)pCtrl->nLine,mtxtinfo.total_lines,r.uWidth);		//Ben 121127#1

						//if ((lidx+lineCount) == mtxtinfo.total_lines)		//Ben 121204#1
    						//if (lineCount == mtxtinfo.total_lines)
    						//	l -= r.uWidth - lastLineWidth;
						}
#endif
					}
				}
				else
				{
					bNewLine = 0;
#ifdef BIDIRECTIONAL_OSD_STYLE
						//lineCount++;	//Ben 121204#1
						if (reverse_flag || mirror_flag)
						{
						RIGHT_LIBC("lineCount=%d lidx=%d nLine=%d total_lines=%d uWidth=%d lastLineWidth=%d \n",
								lineCount,lidx,(UINT8)pCtrl->nLine,mtxtinfo.total_lines,r.uWidth);		//Ben 121127#1
						//Ben 121204#1
						l -=  r.uWidth - lineWidth[lidx];
						//if ((mtxtinfo.total_lines == 1)||(lidx == (mtxtinfo.total_lines-1)))
						////if (lidx == (mtxtinfo.total_lines-1))		
    						//	l -= r.uWidth - lastLineWidth;
						}
#endif
				}    
			}

			if(t>bottom)
				break;

			/* If the first characters of the line is a space, then ignore it.*/
			//            if(l==x && wordlen==1 && wc==' ')
			//            {
			//                pDrawStr += 2;
			//                continue;
			//            }

			/* Draw the word's every characters.*/
			for(k=0;k<wordlen;k++)
			{
				wc = ComMB16ToWord(pDrawStr);
				if(is_thai_unicode(wc))
				{
					struct thai_cell cell;
					UINT32 cnt;

					cnt = thai_get_cell(pDrawStr, &cell);
					if(cnt == 0)
						break;
					if((t + mtxtinfo.line_height[lidx]) <= bottom)
						charw = OSD_DrawThaiCell(l,t ,lpWinSty->wFgIdx,bgColor,&cell, font,pVscr);
					l+= charw;

					pDrawStr += cnt;
					k += cnt/2-1;		
				}
				else
				{
#ifdef BIDIRECTIONAL_OSD_STYLE
//					if (reverse_flag != mirror_flag)
    				if (reverse_flag || mirror_flag)
#else
					if (reverse_flag)
#endif
					{
						OSD_GetCharWidthHeight(wc, font, &charw, &charh);
						l -= charw;
					}
					if((t + mtxtinfo.line_height[lidx]) <= bottom)
					{
						charw = OSD_DrawChar(l,t ,lpWinSty->wFgIdx,bgColor,wc, font,pVscr);
					}
#ifdef BIDIRECTIONAL_OSD_STYLE
//					if (reverse_flag == mirror_flag)
					if (! (reverse_flag || mirror_flag) )
#else
					if (!reverse_flag)
#endif
						l+= charw;

					pDrawStr += 2;
				}
			}
		}
		if(t>=bottom)
			break;
	}
	pVscr->updatePending = 1;
	// TODO : Draw scroll bar
	if(pCtrl->scrollBar != NULL)
	{
		OSD_SetScrollBarMax(pCtrl->scrollBar, mtxtinfo.total_lines);
		OSD_SetScrollBarPage(pCtrl->scrollBar, mtxtinfo.page_lines);
		OSD_SetScrollBarPos(pCtrl->scrollBar,mtxtinfo.topline_idx);
		OSD_DrawObject((POBJECT_HEAD)pCtrl->scrollBar, C_DRAW_SIGN_EVN_FLG);
	}
    
}

VACTION OSD_MultiTextKeyMap(POBJECT_HEAD pObj, UINT32 Key)
{
	VACTION Action = VACT_PASS;
	
	switch(Key)
	{
		case V_KEY_UP:
			Action = VACT_CURSOR_UP;
			break;
		case V_KEY_P_UP:
			Action = VACT_CURSOR_PGUP;
			break;
		case V_KEY_DOWN:
			Action = VACT_CURSOR_DOWN;
			break;
		case V_KEY_P_DOWN:
			Action = VACT_CURSOR_PGDN;
			break;
		default:
			break;
	}

	return Action;
}

PRESULT OSD_MultiTextProc(POBJECT_HEAD pObj, UINT32 msg_type,UINT32 msg,UINT32 param1)
{
	PRESULT Result = PROC_LOOP;
	PMULTI_TEXT pCtrl = (PMULTI_TEXT)pObj;
	INT32 step;
	UINT16 total_line, page_line;
	INT16 line_num;


	if(msg_type == MSG_TYPE_KEY)
	{
		VACTION Action;
		UINT32 vkey;
		BOOL bContinue;
		mtxtinfo_t mtxtInfo;
	    	
		Result = OSD_ObjCommonProc(pObj,msg,param1,&vkey,&Action,&bContinue);
		if(!bContinue)
			goto CHECK_LEAVE;
	
		//total_line = OSD_GetTextLineNum(pCtrl,&page_line);

GET_MULTITEXT_INFOR:    
        mtxtInfo.topline_idx = pCtrl->nLine;
		OSD_GetMTextInfor(pCtrl,&mtxtInfo);
		if(mtxtInfo.total_lines > 0 && mtxtInfo.topline_pstr == NULL)
		{
			pCtrl->nLine = 0;
			goto GET_MULTITEXT_INFOR;
		}
        
		pCtrl->nLine = mtxtInfo.topline_idx;

		line_num = pCtrl->nLine;
		page_line  = mtxtInfo.page_lines;
		total_line = mtxtInfo.total_lines;
            	
		switch(Action)
		{
			case VACT_CURSOR_UP:
				step = -1;
				goto multitext_scroll;
			case VACT_CURSOR_DOWN:
				step = 1;
				goto multitext_scroll;
			case VACT_CURSOR_PGUP:
				step = -page_line;
				goto multitext_scroll;
			case VACT_CURSOR_PGDN:
				step = page_line;
multitext_scroll:
				if(total_line == 0)
					return PROC_LOOP;

				line_num += step;
				if(line_num < 0)
					line_num = 0;

				if(line_num>=total_line)
					line_num -= step;
				
				if(pCtrl->nLine == (UINT16)line_num)
					return PROC_LOOP;
	    		
				Result = OSD_SIGNAL(pObj, EVN_PRE_CHANGE, (UINT32)&line_num, 0);
				if(Result != PROC_PASS)
					goto CHECK_LEAVE;

				pCtrl->nLine = (UINT16)line_num;
				OSD_TrackObject(pObj, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);

				OSD_SIGNAL(pObj, EVN_POST_CHANGE, line_num, 0);
				if(Result != PROC_PASS)
					goto CHECK_LEAVE;
				
				Result = PROC_LOOP;
				break;
		    	case VACT_CLOSE:
CLOSE_OBJECT:    		
		    		Result = OSD_ObjClose(pObj,C_CLOSE_CLRBACK_FLG);
		    		goto EXIT;
				break;
			default:
				Result = OSD_SIGNAL(pObj, EVN_UNKNOWN_ACTION, (Action<<16) | vkey, param1);
				break;
		}
	}
	else// if(osd_msg_type==MSG_TYPE_EVNT)
		Result = OSD_SIGNAL(pObj, msg_type, msg, param1);
	
CHECK_LEAVE:
	if(!(EVN_PRE_OPEN == msg_type))
		CHECK_LEAVE_RETURN(Result,pObj);

EXIT:
	return Result;
}


